<template>
  <view class="order-page">
    <!-- 订单分类标签 -->
    <view class="tab-bar">
      <view 
        v-for="(tab, index) in tabs" 
        :key="index"
        class="tab-item"
        :class="{ active: activeTab === index }"
        @click="activeTab = index"
      >
        {{ tab.text }}
      </view>
    </view>

    <!-- 订单列表 -->
    <view class="order-list">
      <view 
        v-for="(order, index) in filteredOrders" 
        :key="index"
        class="order-item"
      >
        <!-- 商家信息 -->
        <view class="store-info">
          <text class="store-name">{{ order.storeName }}</text>
          <text class="order-id">订单编号: {{ order.orderId }}</text>
          <text class="order-time">{{ order.orderTime }}</text>
          <view class="status-badge" :class="`status-${order.status}`">
            {{ order.statusText }}
          </view>
        </view>

        <!-- 商品列表 -->
        <view class="goods-list">
          <view 
            v-for="(good, goodIndex) in order.goods" 
            :key="goodIndex"
            class="goods-item"
          >
            <image 
              :src="good.image" 
              mode="aspectFill" 
              class="goods-image"
            ></image>
            <view class="goods-content">
              <text class="goods-name">{{ good.name }}</text>
              <text class="goods-spec">{{ good.spec }}</text>
              <text class="goods-price">¥{{ good.price }}</text>
              <text class="goods-count">x{{ good.count }}</text>
            </view>
          </view>
        </view>

        <!-- 订单金额 -->
        <view class="order-amount">
          <view class="amount-item">
            <text class="amount-label">小计</text>
            <text class="amount-value">¥{{ order.subtotal }}</text>
          </view>
          <view class="amount-item">
            <text class="amount-label">配送费</text>
            <text class="amount-value">¥{{ order.deliveryFee }}</text>
          </view>
          <view class="amount-item">
            <text class="amount-label">优惠</text>
            <text class="amount-value">-¥{{ order.discount }}</text>
          </view>
          <view class="amount-item total">
            <text class="amount-label">总计</text>
            <text class="amount-value">¥{{ order.total }}</text>
          </view>
        </view>

        <!-- 操作按钮 -->
        <view class="action-buttons">
          <button 
            v-if="order.status === 'pending'" 
            class="btn-view-delivery"
            @click="viewDelivery(order)"
          >
            查看配送
          </button>
          <button 
            v-if="order.status === 'delivering'" 
            class="btn-contact-store"
            @click="contactStore(order)"
          >
            联系商家
          </button>
          <button 
            v-if="order.status === 'completed'" 
            class="btn-evaluate"
            @click="evaluateOrder(order)"
          >
            去评价
          </button>
          <button 
            v-if="order.status === 'cancelled'" 
            class="btn-reorder"
            @click="reorder(order)"
          >
            再次购买
          </button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue';

// 订单状态枚举
const ORDER_STATUS = {
  PENDING: 'pending',
  DELIVERING: 'delivering',
  COMPLETED: 'completed',
  CANCELLED: 'cancelled'
};

// 订单状态文本映射
const STATUS_TEXT_MAP = {
  [ORDER_STATUS.PENDING]: '待付款',
  [ORDER_STATUS.DELIVERING]: '配送中',
  [ORDER_STATUS.COMPLETED]: '已完成',
  [ORDER_STATUS.CANCELLED]: '已取消'
};

// 订单数据
const orders = ref([
  {
    id: 1,
    storeName: '亚洲小馆',
    orderId: '#20230615008',
    orderTime: '2023-06-15 11:30',
    status: ORDER_STATUS.DELIVERING,
    statusText: STATUS_TEXT_MAP[ORDER_STATUS.DELIVERING],
    subtotal: 28.0,
    deliveryFee: 1.0,
    discount: 10.0,
    total: 19.0,
    goods: [
      {
        id: 1,
        name: '照烧三文鱼饭',
        spec: '规格: 中份 + 少冰可乐',
        price: 23.0,
        count: 1,
        image: '/static/images/salmon-rice.jpg'
      },
      {
        id: 2,
        name: '味增汤',
        spec: '规格: 标准',
        price: 5.0,
        count: 1,
        image: '/static/images/miso-soup.jpg'
      }
    ]
  },
  {
    id: 2,
    storeName: '老北京炸酱面',
    orderId: '#20230614007',
    orderTime: '2023-06-14 18:45',
    status: ORDER_STATUS.COMPLETED,
    statusText: STATUS_TEXT_MAP[ORDER_STATUS.COMPLETED],
    subtotal: 35.0,
    deliveryFee: 1.0,
    discount: 5.0,
    total: 31.0,
    goods: [
      {
        id: 3,
        name: '老北京炸酱面',
        spec: '规格: 大份',
        price: 28.0,
        count: 1,
        image: '/static/images/beijing-noodles.jpg'
      },
      {
        id: 4,
        name: '凉拌黄瓜',
        spec: '规格: 标准',
        price: 7.0,
        count: 1,
        image: '/static/images/cucumber-salad.jpg'
      }
    ]
  },
  {
    id: 3,
    storeName: '川味火锅',
    orderId: '#20230613006',
    orderTime: '2023-06-13 20:15',
    status: ORDER_STATUS.CANCELLED,
    statusText: STATUS_TEXT_MAP[ORDER_STATUS.CANCELLED],
    subtotal: 120.0,
    deliveryFee: 1.0,
    discount: 10.0,
    total: 111.0,
    goods: [
      {
        id: 5,
        name: '麻辣牛肉',
        spec: '规格: 500g',
        price: 60.0,
        count: 1,
        image: '/static/images/spicy-beef.jpg'
      },
      {
        id: 6,
        name: '毛肚',
        spec: '规格: 300g',
        price: 40.0,
        count: 1,
        image: '/static/images/ox-trip.jpg'
      },
      {
        id: 7,
        name: '金针菇',
        spec: '规格: 200g',
        price: 20.0,
        count: 1,
        image: '/static/images/enoki-mushroom.jpg'
      }
    ]
  }
]);

// 分类标签
const tabs = [
  { text: '全部订单', value: 'all' },
  { text: '待评价', value: 'pending-review' },
  { text: '退款/售后', value: 'refund' }
];

// 当前选中的标签
const activeTab = ref(0);

// 过滤后的订单列表
const filteredOrders = computed(() => {
  if (activeTab.value === 0) return orders.value;
  if (activeTab.value === 1) return orders.value.filter(order => order.status === ORDER_STATUS.COMPLETED);
  if (activeTab.value === 2) return orders.value.filter(order => order.status === ORDER_STATUS.CANCELLED);
  return orders.value;
});

// 查看配送
const viewDelivery = (order: any) => {
  uni.showToast({
    title: '查看配送功能待开发',
    icon: 'none'
  });
};

// 联系商家
const contactStore = (order: any) => {
  uni.showToast({
    title: '联系商家功能待开发',
    icon: 'none'
  });
};

// 去评价
const evaluateOrder = (order: any) => {
  uni.showToast({
    title: '去评价功能待开发',
    icon: 'none'
  });
};

// 再次购买
const reorder = (order: any) => {
  uni.showToast({
    title: '再次购买功能待开发',
    icon: 'none'
  });
};

// 页面加载时获取订单数据
onMounted(() => {
  // 这里可以添加从服务器获取订单数据的逻辑
  console.log('订单页面加载完成');
});
</script>

<style scoped>
.order-page {
  background-color: #f5f5f5;
  min-height: 100vh;
}

.tab-bar {
  display: flex;
  background-color: white;
  border-bottom: 1rpx solid #eee;
}

.tab-item {
  flex: 1;
  padding: 20rpx 0;
  text-align: center;
  font-size: 28rpx;
  color: #666;
}

.tab-item.active {
  color: #00C853;
  border-bottom: 2rpx solid #00C853;
}

.order-list {
  padding: 20rpx;
}

.order-item {
  background-color: white;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.store-info {
  padding: 20rpx;
  border-bottom: 1rpx solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.store-name {
  font-size: 32rpx;
  color: #333;
  font-weight: bold;
}

.order-id {
  font-size: 24rpx;
  color: #666;
  margin-top: 4rpx;
}

.order-time {
  font-size: 24rpx;
  color: #666;
  margin-top: 4rpx;
}

.status-badge {
  padding: 4rpx 10rpx;
  border-radius: 12rpx;
  font-size: 20rpx;
  margin-left: 10rpx;
}

.status-pending {
  background-color: #FFD700;
  color: #333;
}

.status-delivering {
  background-color: #00C853;
  color: white;
}

.status-completed {
  background-color: #00C853;
  color: white;
}

.status-cancelled {
  background-color: #FF3B30;
  color: white;
}

.goods-list {
  padding: 20rpx;
  border-bottom: 1rpx solid #eee;
}

.goods-item {
  display: flex;
  margin-bottom: 20rpx;
}

.goods-image {
  width: 100rpx;
  height: 100rpx;
  object-fit: cover;
  border-radius: 8rpx;
}

.goods-content {
  flex: 1;
  margin-left: 20rpx;
}

.goods-name {
  font-size: 28rpx;
  color: #333;
}

.goods-spec {
  font-size: 24rpx;
  color: #666;
  margin-top: 4rpx;
}

.goods-price {
  font-size: 28rpx;
  color: #00C853;
  margin-top: 4rpx;
}

.goods-count {
  font-size: 24rpx;
  color: #666;
  margin-top: 4rpx;
}

.order-amount {
  padding: 20rpx;
  background-color: #f8f8f8;
}

.amount-item {
  display: flex;
  justify-content: space-between;
  padding: 10rpx 0;
  font-size: 28rpx;
}

.amount-label {
  color: #666;
}

.amount-value {
  color: #333;
}

.amount-value::before {
  content: '¥';
}

.total {
  font-weight: bold;
  border-top: 1rpx solid #eee;
  margin-top: 10rpx;
}

.action-buttons {
  padding: 20rpx;
  display: flex;
  gap: 20rpx;
}

.btn-view-delivery {
  width: 140rpx;
  height: 60rpx;
  background-color: #f0f0f0;
  color: #333;
  border-radius: 30rpx;
  font-size: 28rpx;
  line-height: 60rpx;
}

.btn-contact-store {
  width: 140rpx;
  height: 60rpx;
  background-color: #00C853;
  color: white;
  border-radius: 30rpx;
  font-size: 28rpx;
  line-height: 60rpx;
}

.btn-evaluate {
  width: 140rpx;
  height: 60rpx;
  background-color: #00C853;
  color: white;
  border-radius: 30rpx;
  font-size: 28rpx;
  line-height: 60rpx;
}

.btn-reorder {
  width: 140rpx;
  height: 60rpx;
  background-color: #00C853;
  color: white;
  border-radius: 30rpx;
  font-size: 28rpx;
  line-height: 60rpx;
}
</style>